﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WindowsFormsApplication3;


namespace bsy
{
    public partial class Reserve : Form
    {
        private string connStr = Properties.Settings.Default.bsyConnectionString;

        public Reserve()
        {
            InitializeComponent();
        }

        private void Stock_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            All_Reserve_dgv1("");
        }
        //显示所有产品库存的方法
        public void All_Reserve_dgv1(string keyword)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            string sql = "select storeroom_id,p2.PS_name 产品系列,p1.PS_name 产品子系列,number 货号,SR_name 产品名称,guige 容量,price 零售价,count 数量,remark 备注 from storeroom,productsort p1,productsort p2 where p1.P_id=p2.PS_id and storeroom.PS_id=p1.PS_id and SR_name like '%" + keyword + "%'";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.Reserve_dgv1.DataSource = ds.Tables[0].DefaultView;
            this.Reserve_dgv1.Columns[0].Visible = false;
            ds.Dispose();
            conn.Close();
        }

        //private void Reserve_button1_Click(object sender, EventArgs e)
        //{
        //    if (Reserve_SearchtextBox.Text == "" || Reserve_SearchtextBox.Text == "按产品名称关键字搜索……")
        //    {
        //        MessageBox.Show("请输入产品名称关键字！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return;
        //    }
        //    String searchText = Reserve_SearchtextBox.Text.Trim();
        //    String sql = "select count(*) from storeroom,productsort p1,productsort p2 where p1.P_id=p2.PS_id and storeroom.PS_id=p1.PS_id and SR_name like '%" + searchText + "%'";
        //    DataSet ds = new DataSet();
        //    ds = CommonDB.Query(sql);
        //    int count = (int)ds.Tables[0].Rows[0][0];
        //    if (count == 0)
        //    {
        //        MessageBox.Show("该产品不存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //    }
        //    search_Reserve_dgv1();
        //}
        ////搜索功能显示所搜索产品库存的方法
        //public void search_Reserve_dgv1()
        //{
        //    SqlConnection conn = new SqlConnection(connStr);
        //    conn.Open();
        //    String searchText = Reserve_SearchtextBox.Text.Trim();
        //    String sql = "select storeroom_id,p2.PS_name 产品系列,p1.PS_name 产品子系列,number 货号,SR_name 产品名称,guige 容量,price 零售价,count 数量,remark 备注 from storeroom,productsort p1,productsort p2 where p1.P_id=p2.PS_id and storeroom.PS_id=p1.PS_id and SR_name like '%" + searchText + "%'";
        //    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
        //    DataSet ds = new DataSet();
        //    sda.Fill(ds);
        //    this.Reserve_dgv1.DataSource = ds.Tables[0].DefaultView;
        //    this.Reserve_dgv1.Columns[0].Visible = false;
        //    ds.Dispose();
        //    conn.Close();
        //}

        //private void Reserve_ShowAllbutton_Click(object sender, EventArgs e)
        //{
        //    All_Reserve_dgv1();
        //}

        private void Reserve_SearchtextBox_Click(object sender, EventArgs e)
        {
            Reserve_SearchtextBox.Text = "";
        }

        private void Reserve_button1_Click(object sender, EventArgs e)
        {
            String searchText;
            if (Reserve_SearchtextBox.Text == "")
            {
                searchText = "";
            }
           searchText = Reserve_SearchtextBox.Text.Trim();
            All_Reserve_dgv1(searchText);
        }
        //设置单双击只可修改库存表中的产品名称和零售价这两列
        private void Reserve_dgv1_Click(object sender, EventArgs e)
        {
            Reserve_dgv1.Columns[1].ReadOnly = true;
            Reserve_dgv1.Columns[2].ReadOnly = true;
            Reserve_dgv1.Columns[3].ReadOnly = true;
            Reserve_dgv1.Columns[5].ReadOnly = true;
            Reserve_dgv1.Columns[7].ReadOnly = true;
            Reserve_dgv1.Columns[8].ReadOnly = true;
        }
        private void Reserve_dgv1_DoubleClick(object sender, EventArgs e)
        {
            Reserve_dgv1.Columns[1].ReadOnly = true;
            Reserve_dgv1.Columns[2].ReadOnly = true;
            Reserve_dgv1.Columns[3].ReadOnly = true;
            Reserve_dgv1.Columns[5].ReadOnly = true;
            Reserve_dgv1.Columns[7].ReadOnly = true;
            Reserve_dgv1.Columns[8].ReadOnly = true;
        }

        //修改库存中的产品名称和零售价
        private void Reserve_dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int storeroom_id = int.Parse(Reserve_dgv1.CurrentRow.Cells[0].FormattedValue.ToString());
            String SR_name = Reserve_dgv1.CurrentRow.Cells[4].EditedFormattedValue.ToString();
            if (SR_name == "")
            {
                MessageBox.Show("产品名称不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (Reserve_dgv1.CurrentRow.Cells[6].EditedFormattedValue.ToString() == "")
            {
                MessageBox.Show("零售价不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                float price = float.Parse(Reserve_dgv1.CurrentRow.Cells[6].EditedFormattedValue.ToString());
                String sql1 = "update storeroom set SR_name='" + SR_name + "' where storeroom_id='" + storeroom_id + "'";
                String sql2 = "update storeroom set price='" + price + "' where storeroom_id='" + storeroom_id + "'";
                CommonDB.ExecuteSql(sql1);
                CommonDB.ExecuteSql(sql2);
                MessageBox.Show("修改成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Reserve_dgv1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("零售价输入有误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
